package com.kzd.btgame.main.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.entity.GamePlayed
import com.kzd.btgame.entity.GameSmall
import com.kzd.btgame.main.game.adapter.GamePlayedAdapter
import com.kzd.btgame.main.game.adapter.GameSmallAdapter
import razerdp.basepopup.BasePopupWindow


class GameBargainPopup(
    context: Context,
    val onGamePlayed: (view: View, gamePlayed: GamePlayed) -> Unit,
    val onGameSmall: (view: View, gamePlayed: GameSmall) -> Unit
) : BasePopupWindow(context) {

    init {
        popupGravity = Gravity.BOTTOM
        setOverlayNavigationBar(false)
    }

    private val mGamePlayedAdapter = GamePlayedAdapter()

    private val mGameSmallAdapter = GameSmallAdapter()

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_game_bargain)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<ImageView>(R.id.ivGameBargainClose).setOnClickAntiShakeListener {
            dismiss()
        }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 300L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 300L }
    }

    fun fillDataGamePlayed(gamePlayed: MutableList<GamePlayed>): GameBargainPopup {
        contentView.findViewById<TextView>(R.id.tvGameBargainTitle)
            .text = "选择游戏"
        contentView.findViewById<RecyclerView>(R.id.rvGameBargainBody)
            .adapter = mGamePlayedAdapter.apply {
            setNewInstance(gamePlayed)
        }
        mGamePlayedAdapter.setOnItemClickListener { _, view, position ->
            val itemData = mGamePlayedAdapter.getItem(position)
            onGamePlayed(view, itemData)
        }
        return this
    }

    fun fillDataGameSmall(gameSmall: MutableList<GameSmall>): GameBargainPopup {
        contentView.findViewById<TextView>(R.id.tvGameBargainTitle)
            .text = "选择小号"
        contentView.findViewById<RecyclerView>(R.id.rvGameBargainBody)
            .adapter = mGameSmallAdapter.apply {
            setNewInstance(gameSmall)
        }
        mGameSmallAdapter.setOnItemClickListener { _, view, position ->
            val itemData = mGameSmallAdapter.getItem(position)
            onGameSmall(view, itemData)
        }
        return this
    }

}